<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>promise封装ajax</title>
</head>
<body>
<script>
    const p = new Promise((resolve,reject) => {
        const xhr = new XMLHttpRequest();
        xhr.open("GET","https://api.apiopen.top/getJoke");

        xhr.send();

        xhr.onreadystatechange = function (){
            if (xhr.readyState === 4){
                if (xhr.status >= 200 && xhr.status < 300){
                    //表示成功
                    resolve(xhr.response);
                }else {
                    reject(xhr.status);
                }
            }
        }
    })

    //指定回调
    p.then(function (value){
        console.log(value);
    },function (reason){
        console.error(reason);
    })

</script>

</body>
</html>